'use client';

import { CosClient } from '@/lib/cos';
import { useSingleton } from 'foxact/use-singleton';
import React, { useContext } from 'react';

const CosContext = React.createContext<CosClient | null>(null);
export const useCosClient = () => useContext(CosContext);

export default function CosProvider({
  bucket,
  region,
  children,
}: {
  children?: React.ReactNode;
  bucket: string;
  region: string;
}) {
  const cosClient = useSingleton(() => new CosClient({ bucket, region }));
  return <CosContext value={cosClient.current}>{children}</CosContext>;
}
